define(["Zepto", 
        "underscore", 
        "Backbone", 
        "Handlebars",  
        "collections/corsostudentecollection",
        "views/student/librettolistview",
        "text!templates/student/libretto.html",
        "models/session",
        "models/corso",
        "models/utente",
        "views/common/menuview"
], function($, _, Backbone, Handlebars, CorsostudenteCollection,
		LibrettoListView, template, Session, Corso, Utente, MenuView) {
	
   
	  var LibrettoView = Backbone.View.extend({

	        template: Handlebars.compile(template),

	        render: function(eventName) {
	        	
	        	this.user = new Utente(Session.getAuth());
				this.$el.html(this.template(this.user.toJSON()));
				
				// menu top
	        	this.menutop = new MenuView({
	               	el : $('#menu', this.el),
	    			model : this.model
				});
	            this.menutop.render();
	            
	        	
				this.xcorsostudenteCollection = new CorsostudenteCollection();
	            this.corsostudenteCollection = new CorsostudenteCollection();
				this.corsostudenteCollection.fetch({
					query : {
						terminato: true,
						libretto: true,
						utente: {"__type":"Pointer","className":"_User","objectId":this.user.get('objectId')}
	               		},
	               		include:'corso',
	               	success: function(data) {
	                	this.voti = new LibrettoListView({	    				
							el : $('#list', this.el),
							model : data
	    				});
	                
	           
	                	this.voti.render();
	                	
	                	var total = 0; 
	                	var i = 0;
	                 	$('.votli', event.currentTarget.parent).each( function(){
	           			          total += parseInt($(this).text());
	           			          i = i + 1;
	           			});
	                 	$("#mean").text(total/i);
	                	
					}});
				
				
	            return this;
	        },
	        
	  
	  events : {
	 ﻿  ﻿  
		  'touchstart .list-simple' : 'over',
		  'click .updatevote' : 'updatevote'﻿ ,
		  'click .remlibretto' : 'remlibretto'﻿
		  
	  },
		 
	  
	  
	  over: function(event) {
		  
		  $(event.currentTarget).addClass('fake-active');
	    
	  },
	  
	  remlibretto: function(event) {

		  //alert($('.ic', event.currentTarget).text());
		  var course_id = $('.ic', event.currentTarget).text();
		  
		  var r=confirm("Vuoi rimuovere il corso dal libretto?");
		  if (r==true) {
			  
			  this.xcorsostudenteCollection.fetch({
	    		  query : {
	    			  terminato: true,
	    			  libretto: true,
	    			  corso: {"__type":"Pointer","className":"corso","objectId":course_id}		
	    		  },
	    		 	
	    		  success: function(rem) {
	                	if(rem.length > 0){
	                		rem.models[0].set("libretto", false);
	                		rem.models[0].save();
	                		this.render;
	                	}
	           
	    		  }});
		  }else{
		  }
		  
      },
      
      updatevote: function(event) {
    	  
    	  var course_id = $('.ic', event.currentTarget).text();
    	  
    	  this.xcorsostudenteCollection.fetch({
    		  query : {
    			  terminato: true,
    			  libretto: true,
    			  corso: {"__type":"Pointer","className":"corso","objectId":course_id}		
    		  },
    		 	
    		  success: function(rem) {
                	if(rem.length > 0){
                		//alert(rem.models[0].get('votazione'));
                		var voto = prompt("Aggiorna il voto", rem.models[0].get('votazione'));
                		var newvote = parseInt(voto);	 
                		if(isNaN(newvote)){
                			newvote = 0;
                		}
                		//alert('nv ' + newvote);
                		rem.models[0].set("votazione", newvote);
                		rem.models[0].save();
                		$('.votli', event.currentTarget).text(newvote);
                		// PROBLEMA
                		//alert($('.ic', event.currentTarget).text());
                		this.render;
                	}
                	 
                	var total = 0; 
                	var i = 0;
                 	$('.votli', event.currentTarget.parent).each( function(){
           			          total += parseInt($(this).text());
           			          i = i + 1;
           			});
                 	$("#mean").text(total/i);
    		  }});
    	  
    	 
		  
      }
	        
	              
	    
	  });

    return LibrettoView;

});

